﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Routing;
using System.Web.SessionState;
using log4net;
using Imageo.bintje;
using System.Text;

namespace Imageo
{
    public class ProPagesHandler : IHttpHandler, IRequiresSessionState
    {
        #region IHttpHandler Members

        public bool IsReusable
        {
            get { return false; }
        }

        public string prouserid;

        private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        public void ProcessRequest(HttpContext context)
        {
//            string url = String.Format("{0}?{1}",map.Map[context.Request.Url.AbsolutePath],context.Request.Url.Query);
//            context.Server.Transfer("~/ui/ProHome.aspx");
//            string url = context.Request.Url.AbsolutePath;
//            log.DebugFormat("prouser {0}", prouserid);
//            context.Session["prouserid"] = prouserid;

            int proUserIdInt = 0;
            //string strProUserId = context.Session["prouserid"] as string;

            if (!string.IsNullOrEmpty(prouserid))
            {
                if (int.TryParse(prouserid, out proUserIdInt))
                {
                    ServiceAPI service = new ServiceAPI();
                    userRequest ureq = new userRequest();
                    ureq.session = null;
                    ureq.userId = proUserIdInt;
                    if (log.IsErrorEnabled) log.Debug("Calling: service.getUserDetails(ureq)");
                    userResponse uresp = ServiceHelper.HandleBintjeErrors(null, service.getUserDetails(ureq));

                    context.Session["proBackground"] = uresp.user.background;
                    context.Session["proForeground"] = uresp.user.foreground;

                    if (uresp.user.borders)
                    {
                        context.Session["proBorders"] = "true";
                    }
                    else
                    {
                        context.Session["proBorders"] = null;
                    }
                    context.Session["proImageUrl"] = uresp.user.url;

                    context.Server.Transfer("~/ui/Home.aspx?prouserid=" + prouserid);
                }
            }
        }

        #endregion
    }

    public class UrlHelper
    {
        public static string getUrl(string baseUrl, int proUserId, string[] kv)
        {
            StringBuilder result=new StringBuilder(baseUrl);
            if (proUserId > 0)
            {
                result.Append("?prouserid=" + proUserId.ToString());
            }
            else
            {
                if (kv.Length > 0)
                    result.Append("?");
            }
            for (int i = 0; i < kv.Length; i++)
            {
                if (proUserId > 0) result.Append("&");
                result.Append(kv[i]);
            }

            return result.ToString();
        }
    }

    public class RouteHandler : IRouteHandler
    {

        #region IRouteHandler Members

        public IHttpHandler GetHttpHandler(RequestContext requestContext)
        {
            string prouserid = requestContext.RouteData.GetRequiredString("prouserid");

            ProPagesHandler tmp = new ProPagesHandler();
            tmp.prouserid = prouserid;

            return tmp;
        }

        #endregion
    }
}
